package com.vue.vuedemo.controller;

import com.vue.vuedemo.annotation.CheckToken;
import com.vue.vuedemo.map.NewsMapper;
import com.vue.vuedemo.pojo.Lubotu;
import com.vue.vuedemo.pojo.News;
import com.vue.vuedemo.viewpojo.ResList;
import com.vue.vuedemo.viewpojo.ResObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;
import java.util.List;

@RestController
public class NewsController {
    @Autowired
    NewsMapper newsMapper;

    @CheckToken
    @GetMapping("/getnewslist")
    public ResList getAllNews(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        List<News> allNews = newsMapper.getAllNews();
        
        ResList<News> resList = new ResList();
        resList.setStatus(0);
        resList.setMessage(allNews);
        
        return resList;
    }

    @CheckToken
    @GetMapping("/getnew/{id}")
    public ResList getNewsInfo(@PathVariable("id") Integer id,HttpServletResponse response){
//        System.out.println(id);
        response.setHeader("Access-Control-Allow-Origin", "*");

        News newInfo = newsMapper.getNewInfo(id);
        List<News> list = new ArrayList<>();
        list.add(newInfo);
        
        ResList<News> resList = new ResList();
        resList.setStatus(0);
        resList.setMessage(list);
        
        
        return resList;
    }
}
